﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using RFMobile.Database;
using RFMobile.Machines;

namespace RFMobile
{
    public partial class historyScreen : Form
    {
        MachineData machine;
        DatabaseManager manHistory;

        Stack<Int32> id_operation;

        public historyScreen(Int32 _comtec_id_machine)
        {
            machine = new MachineData(_comtec_id_machine);

            manHistory = new DatabaseManager();

            id_operation = new Stack<Int32>();

            InitializeComponent();

            this.ShowDialog();
        }

        private void historyScreen_Load(object sender, EventArgs e)
        {
            statusBar1.Text = "Máquina : " + machine.cod_mainteiner.ToString();

            manHistory.BuildDataSet(new String[] {"operation", "users"} , "historySet");

            DataTable history = manHistory.Ds.Tables["operation"];

            foreach (DataRow drCurrent in history.Rows)
            {
                if (drCurrent["comtec_id_machine"].ToString().Equals(machine.comtec_id_machine.ToString()))
                {
                    dateComboBox.Items.Add(drCurrent["operation_date_start"].ToString());
                    id_operation.Push(Convert.ToInt32(drCurrent["comtec_id_operation"]));
                }
            }

            // Orden de array
            Int32[] aux = id_operation.ToArray();
            id_operation.Clear();
            for (Int32 i = 0; i < aux.Length; i++)
            {
                id_operation.Push(aux[i]);
            }
            aux = null;
        }

        private void dateComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 id = id_operation.ToArray()[dateComboBox.SelectedIndex];
            DataRow opDr = manHistory.Ds.Tables["operation"].Rows.Find(id);
            DataRow usrDr = manHistory.Ds.Tables["users"].Rows.Find(opDr["comtec_id_user"]);

            codTextBox.Text = opDr["comtec_id_operation"].ToString();
            typeOperationTextBox.Text = operationType(opDr["operation_type"]);
            responsibleTextBox.Text = usrDr["name"].ToString() + " " + usrDr["lastname1"].ToString();
            diagnosisTextBox.Text = opDr["diagnosis"].ToString();
            changedPartsTextBox.Text = opDr["changed_parts"].ToString();
            notesTextBox.Text = opDr["notes"].ToString();

            String[] dateIni;
            String[] dateFin;

            dateIni = opDr["operation_date_start"].ToString().Split(new Char[] {' '} );
            dateFin = opDr["operation_date_end"].ToString().Split(new Char[] {' '});

            fecha_ini_TextBox.Text = dateIni[0];
            fecha_fin_TextBox.Text = dateFin[0];

            hora_ini_TextBox.Text = dateIni[1];
            hora_fin_TextBox.Text = dateFin[1];

            dateIni = null;
            dateFin = null;

            fecha_prox_op_TextBox.Text = machine.date_next_maintenance.ToString();

            opDr = null;
            usrDr = null;
        }

        private String operationType(object i)
        {
            if (i.ToString().Equals("1"))
                return ("Mantencion");
            if (i.ToString().Equals("2"))
                return ("Emergencia");
            if (i.ToString().Equals("3"))
                return ("Instalacion");
            return ("");
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}